If you are a professional .NET software engineer, the chances are extremely good that your employer has purchased Microsoft's premier IDE, Visual Studio 2010, for your development endeavors (http://msdn.microsoft.com/vstudio). This tool is far and away the most feature-rich, enterprise-ready IDE examined in this chapter. Of course, this power comes at a price, which will vary based on the version of Visual Studio 2010 you purchase. As you might suspect, each version supplies a unique set of features.
Note There is a staggering number of members within the Visual Studio 2010 family. My assumption during the remainder of this text is that you have chosen to make use of Visual Studio 2010 Professional as your IDE of choice.
Although I will assume you have a copy of Visual Studio 2010 Professional, understand that owning a copy of this IDE is not required to use this edition of the text. In the worst case, I may examine an option that is not provided by your IDE. However, rest assured that all of this book's sample code will compile just fine when processed by your tool of choice.
Note Once you download the source code for this book from the Source Code/Downloads area of the Apress web site (http://www.apress.com), you may load the current example into Visual Studio 2010 (or C# 2010 Express) by double-clicking the example's *.sln file. If you are not using Visual Studio 2010/C# 2010 Express, you will need to manually insert the provided *.cs files into your IDE's project work space.
Visual Studio 2010 ships with the expected GUI designers, code snippet support, database manipulation tools, object and project browsing utilities, and an integrated help system. Unlike many of the IDEs we have already examined, Visual Studio 2010 provides numerous additions. Here is a partial list.
To be completely honest, Visual Studio 2010 provides so many features that it would take an entire book (a rather large book at that) to fully describe every aspect of the IDE. This is not that book. However, I do want to point out some of the major features in the pages that follow. As you progress through the text, you'll learn more about the Visual Studio 2010 IDE where appropriate.
If you are following along, create a new C# Console Application (named Vs2010Example) using the File ? New ? Project menu item. As you can see in Figure 2-9, Visual Studio 2010 supports the ability to select which version of the .NET Framework you wish to build against (2.0, 3.x, or 4.0) using the dropdown list box on the top center of the New Project dialog box. For each project in this text, you can simply leave the default selection of .NET Framework 4.0.
Figure 2-9 Visual Studio 2010 allows you to target a particular version of the .NET Framework
The Solution Explorer utility (accessible from the View menu) allows you to view the set of all content files and referenced assemblies that comprise the current project (see Figure 2-10).
Figure 2-10 The Solution Explorer utility
Notice that the References folder of Solution Explorer displays a list of each assembly you have currently referenced, which will differ based on the type of project you select and the version of the Framework you are compiling against.
When you need to reference additional assemblies, right-click the References folder and select Add Reference. At this point, you can select your assembly from the resulting dialog box (this is essentially the way Visual Studio allows you to specify the /reference option of the command-line compiler). The .NET tab (see Figure 2-11) displays a number of commonly used .NET assemblies; however, the Browse tab allows you to navigate to any .NET assembly on your hard drive. Also, the very useful Recent tab keeps a running tally of frequently referenced assemblies you have used in other projects.
Figure 2-11 The Add Reference dialog box
Finally, notice an icon named Properties within Solution Explorer. When you double-click this item, you are presented with a sophisticated project configuration editor (see Figure 2-12).
Figure 2-12 The Project Properties window
You will see various aspects of the Project Properties window as you progress through this book. However, if you take some time to poke around, you will see that you can establish various security settings, strongly name your assembly, deploy your application, insert application resources, and configure pre- and post-build events.
The next tool to examine is the Class View utility, which you can load from the View menu. The purpose of this utility is to show all of the types in your current project from an object-oriented perspective (rather than a file-based view of Solution Explorer). The top pane displays the set of namespaces and their types, while the bottom pane displays the currently selected type's members (see Figure 2-13).
Figure 2-13 The Class View utility
When you double click on a type or type member using the Class View tool, Visual Studio will automatically open the correct C# code file, and place your mouse cursor at the correct location. Another nice feature of the Visual Studio 2010 Class View tool is that you are able to open up any referenced assembly and drill into the contained namespaces, types, and members (Figure 2-14).
Figure 2-14 The Class View utility can also be used to view referenced assemblies
Visual Studio 2010 also provides a second utility to investigate the set of referenced assemblies within your current project. Activate the Object Browser using the View menu, and then select the assembly you wish to investigate (see Figure 2-15).
Figure 2-15 The Object Browser utility
One major feature that ships with Visual Studio 2010 is support to refactor existing code. Simply put, refactoring is a formal and mechanical process whereby you improve an existing code base. In the bad old days, refactoring typically involved a ton of manual labor. Luckily, Visual Studio 2010 does a great deal to automate the refactoring process.
Using the Refactor menu (which will only be available when a code file is active), related keyboard shortcuts, smart tags, and/or context-sensitive mouse clicks, you can dramatically reshape your code with minimal fuss and bother. Table 2-2 defines some common refactorings recognized by Visual Studio 2010.
Table 2-2. Visual Studio 2010 Refactorings
Refactoring Technique | Meaning in Life |
---|---|
Extract Method | Allows you to define a new method based on a selection of code statements |
Encapsulate Field | Turns a public field into a private field encapsulated by a C# property |
Extract Interface | Defines a new interface type based on a set of existing type members |
Reorder Parameters | Provide a way to reorder member arguments |
Remove Parameters | Remove a given argument from the current list of parameters (as you would expect) |
Rename | Allows you to rename a code token (method name, field, local variable, and so on) throughout a project |
To illustrate refactoring in action, update your Main() method with the following simple code:
static void Main(string[] args) { // Set up Console UI (CUI) Console.Title = "My Rocking App"; Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.Blue; Console.WriteLine("*************************************"); Console.WriteLine("***** Welcome to My Rocking App *****"); Console.WriteLine("*************************************"); Console.BackgroundColor = ConsoleColor.Black; // Wait for Enter key to be pressed. Console.ReadLine(); }
While there is nothing wrong with the preceding code as it now stands, imagine that you want to display this welcome message at various places throughout your program. Rather than retyping the same exact console user interface logic, it would be ideal to have a helper function that could be called to do so. Given this, you will apply the Extract Method refactoring to your existing code.
First, select each code statement within Main() (except the final call to Console.ReadLine()) using the editor. Then right-click the selected text and select the Extract Method option within the Refactor context menu (see Figure 2-16).
Figure 2-16 Activating a code refactoring
Name your new method ConfigureCUI using the resulting dialog box. When you have finished, you will find that your Main() method calls the newly generated ConfigureCUI() method, which now contains the previously selected code.
class Program { static void Main(string[] args) { ConfigureCUI(); // Wait for key press to close. Console.ReadLine(); } private static void ConfigureCUI() { // Set up Console UI (CUI) Console.Title = "My Rocking App"; Console.ForegroundColor = ConsoleColor.Yellow; Console.BackgroundColor = ConsoleColor.Blue; Console.WriteLine("*************************************"); Console.WriteLine("***** Welcome to My Rocking App *****"); Console.WriteLine("*************************************"); Console.BackgroundColor = ConsoleColor.Black; } }
This is a simple example of using the built-in refactorings of Visual Studio 2010, and you'll see additional examples here and there over the course of this text.
Visual Studio 2010 (as well as Visual C# 2010 Express) is capable of inserting prefabricated blocks of C# code using menu selections, context-sensitive mouse clicks, and/or keyboard shortcuts. The number of available code expansions is impressive and can be broken down into two main groups.
To see this functionality firsthand, assume that you wish to iterate over the incoming parameters of the Main() method using a foreach construct. Rather than typing the code in by hand, you can activate the foreach code snippet. When you have done so, the IDE will dump out a foreach code template at the current location of the mouse cursor.
To illustrate, place the mouse cursor after the initial opening curly bracket of Main(). One way to activate a code snippet is to right-click the mouse and activate the Insert Snippet (or Surround With) menu option. Here, you will find a list of all code snippets of this category (press the Esc key to dismiss the pop-up menu). As a shortcut, however, you can simply type in the name of the code snippet, foreach in this case. In Figure 2-17, notice how the icon for a code snippet looks a bit like a torn piece of paper.
Figure 2-17 Activating a code snippet
Once you find the snippet you want to activate, press the Tab key twice. This will autocomplete the entire snippet and leave a set of placeholders that you can fill in to complete the snippet. If you press the Tab key, you can cycle between each placeholder and fill in the gaps (press the Esc key to exit the code snippet edit mode).
If you were to right-click and select the Surround With menu, you would likewise be presented with a list of options. Recall that when using Surround With snippets, typically you first select a block of code statements to represent what should be used to wrap them (e.g., try/catch block). Be sure to take time to explore these predefined code expansion templates, as they can radically speed up the development process.
Note All code expansion templates are XML-based descriptions of the code to generate within the IDE. Using Visual Studio 2010 (as well as Visual C# 2010 Express), you can create your own custom code templates. Details of how to do so can be found in my article 'Investigating Code Snippet Technology' at http://msdn. microsoft.com.
Visual Studio 2010 gives us the ability to design classes visually (this capability is not included in Visual C# 2010 Express). The Class Designer utility allows you to view and modify the relationships of the types (classes, interfaces, structures, enumerations, and delegates) in your project. Using this tool, you are able to visually add (or remove) members to (or from) a type and have your modifications reflected in the corresponding C# file. Also, as you modify a given C# file, changes are reflected in the class diagram.
To work with this aspect of Visual Studio 2010, the first step is to insert a new class diagram file. There are many ways to do so, one of which is to click the View Class Diagram button located on Solution Explorer's right side (see Figure 2-18; be sure your project - not the solution - is the selected item in the window).
Figure 2-18 Inserting a class diagram file
Once you do, you will find class icons that represent the classes in your current project. If you click the arrow icon for a given type, you can show or hide the type's members (see Figure 2-19).
Note Using the Class Designer toolbar, you can fine-tune the display options of the designer surface.
Figure 2-19 The Class Diagram viewer
This utility works in conjunction with two other aspects of Visual Studio 2010: the Class Details window (activated using the View ? Other Windows menu) and the Class Designer Toolbox (activated using the View ? Toolbox menu item). The Class Details window not only shows you the details of the currently selected item in the diagram, but also allows you to modify existing members and insert new members on the fly (see Figure 2-20).
Figure 2-20 The Class Details window
The Class Designer Toolbox, which can be activated using the View menu, allows you to insert new types into your project (and create relationships between these types) visually (see Figure 2-21). (Be aware you must have a class diagram as the active window to view this toolbox.) As you do so, the IDE automatically creates new C# type definitions in the background.
Figure 2-21 The Class Designer Toolbox
By way of example, drag a new Class from the Class Designer Toolbox onto your Class Designer. Name this class Car in the resulting dialog box. Now, using the Class Details window, add a public string field named petName (see Figure 2-22).
Figure 2-22 Adding a field with the Class Details window
If you now look at the C# definition of the Car class, you will see it has been updated accordingly (minus the additional code comments).
public class Car { // Public data is typically a bad idea; however, // it keeps this example simple. public string petName; }
Now, activate the designer file once again and drag another new Class onto the designer named SportsCar. Now, select the Inheritance icon from the Class Designer Toolbox and click the top of the SportsCar icon. Next, click the mouse on top of the Car class icon. If you performed these steps correctly, you have just derived the SportsCar class from Car (see Figure 2-23).
Figure 2-23 Visually deriving from an existing class
To complete this example, update the generated SportsCar class with a public method named GetPetName() authored as follows:
public class SportsCar : Car { public string GetPetName() { petName = "Fred"; return petName; } }
You will make use of these (and other) visual tools of Visual Studio 2010 over the course of this book. However, you should now feel a bit more comfortable regarding the basics of the IDE.
Note The concept of inheritance will be fully examined in Chapter 6.
The final aspect of Visual Studio 2010 you must be comfortable with from the outset is the fully integrated help system. The .NET Framework 4.0 SDK documentation is extremely good, very readable, and full of useful information. Given the huge number of predefined .NET types (which number well into the thousands), you must be willing to roll up your sleeves and dig into the provided documentation. If you resist, you are doomed to a long, frustrating, and painful existence as a .NET developer.
If you have an internet connection, you can view the .NET Framework 4.0 SDK documentation online at the following web address:
http://msdn.microsoft.com/library
Of course, if you do not have a live internet connection, this is not very useful. Thankfully, you can install the same help system locally to your computer. Assuming you have already installed Visual Studio 2010, navigate to the All Programs | Microsoft Visual Studio 2010 | Visual Studio Tools folder using your Windows Start button, and select the Manage Help Settings tool. Once you have done so, you can then elect to add the help documentation you are interested in, as seen in Figure 2-24 (if hard drive space allows, I'd recommend adding all possible documentation).
Figure 2-24 The Help Library Manager allows you to download a local copy of the .NET Framework 4.0 SDK Documentation
Note Beginning with .NET 4.0, the help system is viewed via your current web browser, even if you do a local installation of the .NET Framework 4.0 SDK documentation.
Once the local help system is installed, the simplest way to interact with the documentation is to select a C# keyword, type name or member name within the Visual Studio 2010 code window, and press the F1 key. This will automatically open a documentation window for the selected item. For example, select the string keyword within your Car class definition. Once you press F1, you will see the Help page appear.
Another useful aspect of the documentation is the Search edit box located on the upper left area of the display. Here you can enter in the name of any namespace, type, or member and navigate to the correct location. If you were to try to search for the System.Reflection namespace, you would be able to learn about the details of this namespace, examine the contained types, view code examples, and so forth (Figure 2-25)
Figure 2-25 The Search edit box allows you to look up items of interest
Each node in the tree defines the set of types in a given namespace, the members of a given type, and the parameters of a given member. Furthermore, when you view the help page for a given type, you will be told the name of the assembly and namespace that contains the type in question (located at the top of said page). As you read through the remainder of this book, I assume that you will dive into this very, very critical node to read up on additional details of the entity under examination.
Note At the risk of sounding like a broken record, I really can't emphasize enough how important it is that you learn to use the .NET Framework 4.0 SDK documentation. No book, no matter how lengthy, can cover every aspect of the .NET platform. Make sure you take some time to get comfortable using the help system'you'll thank yourself later.